<?php
/**
 * Copyright 2011  SURFfoundation
 * 
 * This file is part of ESCAPE.
 * 
 * ESCAPE is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * ESCAPE is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with ESCAPE.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * For more information:
 * http://escapesurf.wordpress.com/
 * http://www.surffoundation.nl/
 * 
 * Contact: d.vierkant@utwente.nl
 * 
 * @license http://www.gnu.org/licenses/gpl.html GNU GPLv3
 */
?>
<?php
/**
 * Clone
 * 
 * Clone an aggregation.
 */
class escape_repositoryui_action_Clone extends escape_repositoryui_Action
{
	public function handleRequest()
	{
		/* @var $repository escape_repository_Repository */
		$repository =& $this->requestAttributes['repository'];
		/* @var $repositoryUi escape_repositoryui_RepositoryUi */
		$repositoryUi =& $this->requestAttributes['repositoryUi'];
			
		// fetch the ID from the request
		$objectId = $_REQUEST['actionValue'];
		$objectUri = $repository->convertDataObjectIdToObjectUri($objectId);
		
		// fetch the object
		try {
			$aggregation = $repository->getOreAggregationByUri($objectUri);
		}
		catch(Exception $exception)
		{
			$repositoryUi->displayErrorPage($exception->getMessage());
			return null;
		}

		$this->requestAttributes['aggregation'] =& $aggregation;
		
		// compose a return URL
		$returnUrl = $repository->config['server_url'] . $repositoryUi->getActionLinkById('show', $aggregation->getId());
	
		if(isset($_REQUEST['return_url']))
		{
			$returnUrl = $_REQUEST['return_url'];
		}
		
		$this->requestAttributes['returnUrl'] = $returnUrl;
		
		// check if the user is authorized to clone this aggregation
		$user = $repositoryUi->getUser();
		$userIsAuthorized = false;
		if($user)
		{
			$userUri = $user->getUri();
			$userIsAdministrator = $user->hasSystemRole(escape_repository_User::$role_administrator);
			
			if($userIsAdministrator || ($aggregation->hasOwner($userUri) && $user->hasSystemRole(escape_repository_User::$role_creator)))
			{
				$userIsAuthorized = true;
			}
		}
	
		if(!$userIsAuthorized)
		{
			// user is not authorized to edit this resource, redirect to login page
			$repositoryUi->redirectToLoginForAction($this->requestAttributes['actionName'], $objectId);
			return null;
		}
		
		// check if the user confirmed the delete action
		$cloneConfirmed = false;
		if(isset($_POST['clone_confirm']) && $_POST['clone_confirm'] == '1')
		{
			$cloneConfirmed = true;
		}
		
		if($cloneConfirmed)
		{
			// delete the object
			$newAggregation = $aggregation->cloneAggregation();
			
			// update the title
			$newTitle = $_REQUEST['title'];
			if($newTitle)
			{
				$oldTitleTriple = $aggregation->getTitle(true);
				$rdfResource = $newAggregation->getProperties();
				$rdfResource->replaceObject($oldTitleTriple['P'], $oldTitleTriple['o'], $newTitle);
				$newAggregation->setProperties($rdfResource);
			}
			
			// add the current user as the owner of the cloned aggregation
			$newAggregation->addOwner($user->getUri());
			
			$returnUrl = $repository->config['server_url'] . $repositoryUi->getActionLinkById('show', $newAggregation->getId());
			if($repositoryUi->isGadgetMode())
			{
				$returnUrl .= '?gadget=1';
			}
			header("Location: " . $returnUrl);
			return null;
		}
		
		return 'AggregationCloneConfirm';
	}
}